package org.china.lanzhou.neo4j.serveice;

import org.china.lanzhou.neo4j.Neo4jUtil;
import org.china.lanzhou.neo4j.entity.CQL;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Neo4jService {

    // 创建节点
    public  Integer createNode(CQL cql) {
        StringBuffer neoCreate = new StringBuffer();
        neoCreate.append("create (o:");
        neoCreate.append(cql.getNode());
        neoCreate.append("{");
        neoCreate.append(cql.getProperty());
        neoCreate.append("}) return ID(o) as id ");
        Integer id = null;
        Statement statement = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            conn = Neo4jUtil.getConnection();
            statement = conn.createStatement();
            rs = statement.executeQuery(neoCreate.toString());
            id = null;
            while (rs.next()) {
                id = rs.getInt("id");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                Neo4jUtil.freeConnection(rs, statement, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return id;
    }

    // 获取节点
    public  Result getNode(CQL cql) {
//        String neoMatch = "match (o: " + type + " {name:\"" + nodeName + "\"}) return o.name,ID(o) as id";
        StringBuffer neoMatch = new StringBuffer();
        neoMatch.append("match (o: ");
        neoMatch.append(cql.getNode());
        neoMatch.append("{");
        neoMatch.append(cql.getProperty());
        neoMatch.append("}) return o.name,ID(o) as id");
        Result result = null;
        Statement statement = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            conn = Neo4jUtil.getConnection();
            statement = conn.createStatement();
            rs = statement.executeQuery(neoMatch.toString());
            result = new Result();
            while (rs.next()) {
                int id = rs.getInt("id");
                result.put("id", id);
                for (String s : cql.getResult().split(",")) {
                    result.put(s, rs.getObject(s));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                Neo4jUtil.freeConnection(rs, statement, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    // 添加关注关系
    public  void addRelationShip(Integer orgId, Integer personId, String relationType,String relationProperty) {
//        String neoRelation = "start a=node(" + orgId + "),b=node(" + personId + ") create (a)-[n:"+relationType+"]->(b)";
        StringBuffer neoRelation = new StringBuffer();
        neoRelation.append("start a=node(");
        neoRelation.append(orgId);
        neoRelation.append("),b=node(");
        neoRelation.append(personId);
        neoRelation.append(") create (a)-[n:");
        neoRelation.append(relationType);
        neoRelation.append("{"+relationProperty);
        neoRelation.append("}]->(b)");
        execute(neoRelation.toString());
    }

    // 删除节点
    public  void delete(String node, String property){
        StringBuffer neoCreate = new StringBuffer();
        neoCreate.append("MATCH (o:");
        neoCreate.append(node);
        neoCreate.append("{");
        neoCreate.append(property);
        neoCreate.append("'}) delete o ");
        execute(neoCreate.toString());
    }

    public  void remove(String node, String property,String removeProperty){
        StringBuffer neoCreate = new StringBuffer();
        neoCreate.append("MATCH (o:");
        neoCreate.append(node);
        neoCreate.append("{");
        neoCreate.append(property);
        neoCreate.append("'}) remove o."+removeProperty);
        execute(neoCreate.toString());
    }


    public  void execute(String cql) {
        Connection conn = null;
        Statement statement = null;
        try {
            conn = Neo4jUtil.getConnection();
            statement = conn.createStatement();
            statement.execute(cql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                Neo4jUtil.freeConnection(null, statement, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public  void executeQuery(String cql,QueryCallBack queryCallBack) {
        Statement statement = null;
        Connection conn = null;
        ResultSet resultSet = null;
        try {
            conn = Neo4jUtil.getConnection();
            statement = conn.createStatement();
            resultSet = statement.executeQuery(cql);
            queryCallBack.callBack(resultSet);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                Neo4jUtil.freeConnection(resultSet, statement, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
